<!doctype html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>管理员管理</title>

    <link href="/css/common.css" rel="stylesheet">
    <link href="/css/main-header.css" rel="stylesheet">
    <link href="/css/main-orderForm-tab.css" rel="stylesheet">
    <link href="/css/adminManage.css" rel="stylesheet">

    <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.2/style/weui.min.css">
    <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.0/css/jquery-weui.min.css">

</head>

<body>

<% include main-header.ejs %>

<div class="main1">
    <div class="main-center clearfix">

        <div id="adminListBox" class="adminListBox clearfix"></div>


        <form class="main1-form" id="main1-form">
            <div class="weui-cells weui-cells_form">

                <div class="weui-cell">
                    <div class="weui-cell__hd">
                        <label class="weui-label" for="main1-form-username">QQ号</label>
                    </div>
                    <div class="weui-cell__bd">
                        <input
                                class="weui-input"
                                id="main1-form-username"
                                name="username"
                                type="Number"
                                placeholder="请输入QQ号"
                        >
                    </div>
                </div><!--weui-cell-->

                <input class="weui-btn weui-btn_primary" id="main1-form-addAdmin" type="submit" value="添加管理">
            </div>


        </form>






    </div>
</div>

<script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-weui/1.2.0/js/jquery-weui.min.js"></script>
<script src="https://cdn.bootcss.com/jquery-validate/1.17.0/jquery.validate.min.js"></script>

<script src="/js/main-header.js"></script>

<script>

    $(document).ready(function() {


        // 请求数据

            $.post("/getUserList", {
                "findObj": '{' +
                    '"userType": "admin"' +
                '}'
            }, function(result) {

                // console.log(result)

                iterator(0);

                function iterator(i) {
                    if (i === result.length) { // 迭代结束
                        //在这里写迭代结束后的内容
                        return;
                    }

                    // 组装模版
                    if (result[i].of_name == undefined) {
                        var user =
                            '<div class="user-item clearfix">' +
                                '<a>' + result[i].username + '</a>' +
                                '<a href="/userType/' + result[i]._id + '">' + '删除' + '</a>'
                            '</div>';
                    } else {
                        var user =
                            '<div class="user-item clearfix">' +
                            '<a>' + result[i].username + '</a>' +
                            '<a>' + result[i].of_name + '</a>' +
                            '<a href="/userType/' + result[i]._id + '">' + '删除' + '</a>'
                        '</div>';
                    }


                    $("#adminListBox").append($(user));

                    // 执行下一次循环
                    iterator(i + 1);
                }
            })


        $("#main1-form").validate({
            rules: {
                username: {
                    required: true,
                    digits: true
                }
            },
            messages: {
                username: {
                    required: "请填写您的常用QQ号码",
                    digits: "请填写正确的QQ号码"
                }
            },
            focusInvalid: true,
            submitHandler: function () {
                var username = $("#main1-form-username").val().toString()

                $.get("/addAdmin/" + username, function (result) {
                    if (result === "1"){  // 成功
                        window.location = "/adminManage";
                    }
                })
            },
            errorElement: "p"  // 错误标签用<p>
        })


    })


    function timestampToTime(timestamp) {
        var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        Y = date.getFullYear() + '-';
        M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        D = date.getDate() + ' ';
        h = date.getHours() + ':';
        m = date.getMinutes() + ':';
        s = date.getSeconds();
        return Y + M + D + h + m + s;
    }
</script>

</body>

</html>